import ArrayUtil from '@/ArrayUtil';
import Buff from '@/Buff';
import BuffUtil from '@/BuffUtil';
import SelectUtil from '@/SelectUtil';
import BuffCommon from 'code/Core/Common/BuffCommon';
import MovementCommon from 'code/Core/Common/MovementCommon';
import SLKCommon from 'code/Core/Common/SLKCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 高射火炮
 **/
export default class YX_ArchiBald extends AbilityBase {
    static instance: YX_ArchiBald;
    buffType: AppBuffType = {
        id: 'buff_YX_ArchiBald',
        art: this.art,
        name: this.abilityName,
        ubertip: this.ubertip,
        dur: 12,
        stackCountMax: -1,
        targetArt: 'Abilities\\Spells\\Human\\FlakCannons\\FlakTarget.mdl',
        targetAttach: 'weapon',
        onDestroy: this.buffDestroy.bind(this),
    };
    buffDestroy(buff: Buff) {
        UnitRemoveAbility(buff.getParent(), 'Aflj');
    }
    constructor(id: string = 'Agsp') {
        super(id);
        YX_ArchiBald.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
        EventManager.onTakeAttack(this.attackEffect.bind(this));
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let lv = this.getAbilityLevel(source);
        let stack = lv + 1;
        BuffCommon.addBuff({
            target: source,
            caster: source,
            buffTypeId: this.buffType.id,
            ablityId: this.id,
            config: { stack: stack },
        });
        UnitAddAbility(source, 'Aflj');
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    attackEffect(d: DamageEventData) {
        if (!d.isAttack) return;
        if (GetUnitAbilityLevel(d.source, 'Aflj') == 0) return;
        if (d.source != d.target) return;
        d.lastDamage = 0;
        let buff = BuffUtil.getUnitBuff(d.source, this.buffType.id);
        if (!buff) return;
        buff.stackCount -= 1;
        let range = GetUnitState(d.source, UnitStateDamageRange) + 250;
        let speed = SLKCommon.getUnitMissileSpeed(d.source);
        let missile = SLKCommon.getUnitMissileart(d.source);
        let radian = SLKCommon.getUnitMissilearc(d.source);
        let es = SelectUtil.getEnemyUnitsInRange(d.source, range);
        for (let e of es) {
            MovementCommon.TargetMissile({
                missile,
                target: e,
                caster: d.source,
                speed,
                radian,
                onReach: this.missileReach.bind(this),
            });
        }
    }
    missileReach(p: TargetMissileConfig) {
        let dmg = UnitCommon.取攻击(p.caster);
        DamageSystemInstance.applyAttackDamage(p.target, p.caster, dmg, this.id);
    }
}
